package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.CharlaPProg;
import co.gov.medellin.sdep.sisdep.model.Citacion;
import co.gov.medellin.sdep.sisdep.model.dao.CitacionDAOImpl.ValidarDocumentoOperation;
import co.gov.medellin.sdep.sisdep.model.dao.CitacionDAOImpl.VerificarDocumentoQuery;

public class CharlaPProgDAOImpl extends JdbcDaoSupport implements ICharlaPProgDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private VerificarDocumentoQuery verificarDocumentoQuery;
    
    private FindAllQuery findAllQuery;
    
    private FindByTipoCharlaQuery findByTipoCharlaQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM OPE_CHARLA_P_PROG_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(CharlaPProg charlaPProg) {
		deleteOperation.execute( charlaPProg  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public CharlaPProg findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(CharlaPProg charlaPProg) {
		insertOperation.execute( charlaPProg );
	}

	public void update(CharlaPProg charlaPProg) {
		updateOperation.execute( charlaPProg );
	}

	public CharlaPProg findByCriteria(CharlaPProg  charlaPProg) {
		return findByCriteriaQuery.runQuery(charlaPProg);
	}

	public List load() {
		return findAllQuery.runQuery();
	}
	
	public List loadByTipoCharla(CharlaPProg  charlaPProg) {
		return findByTipoCharlaQuery.runQuery(charlaPProg );
	}
	
	public String verificarDocumento(CharlaPProg  charlaPProg) {
		return verificarDocumentoQuery.runQuery(charlaPProg);
	}

	public CharlaPProg findByActiveState(CharlaPProg  charlaPProg) {
		return findByActiveStateQuery.runQuery(charlaPProg);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByTipoCharlaQuery = new FindByTipoCharlaQuery( getDataSource() );
    	verificarDocumentoQuery = new VerificarDocumentoQuery( getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO OPE_CHARLA_P_PROG_T ("
				  + " ID, "
				  + " ID_TIPO_CHARLA, "
				  + " TIPO_DTO_FUNCIONARIO, "
				  + " DTO_FUNCIONARIO, "
				  + " TEMA, "
				  + " DESCRIPCION, "
				  + " FECHA, "
				  + " HORA, "
				  + " ID_LUGAR, "
				  + " OTRO_LUGAR, "
				  + " UNIDAD_DURACION, "
				  + " DURACION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " OPE_CHARLA_P_PROG_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(CharlaPProg charlaPProg) {
			InsertOperation.this.update( new Object[]{				
				charlaPProg.getIdTipoCharla(),
				charlaPProg.getIdTipoDtoStr(),
				charlaPProg.getDocumentoStr(),				
				charlaPProg.getTema(),
				charlaPProg.getDescripcion(),
				charlaPProg.getFecha(),
				charlaPProg.getHora(),
				charlaPProg.getIdLugar(),
				charlaPProg.getOtro(),
				charlaPProg.getUnidadDuracion(),
				charlaPProg.getDuracion(),
				charlaPProg.getEstado(),
				charlaPProg.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE OPE_CHARLA_P_PROG_T SET "
					  + " ID_TIPO_CHARLA = ? ,"
					  + " TIPO_DTO_FUNCIONARIO=? ,"
					  + " DTO_FUNCIONARIO = ? ,"
					  + " TEMA = ? ,"
					  + " DESCRIPCION = ? ,"
					  + " FECHA = ? ,"
					  + " HORA = ? ,"
					  + " ID_LUGAR = ? ,"
					  + " OTRO_LUGAR = ? ,"
					  + " UNIDAD_DURACION = ? ,"
					  + " DURACION = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
					  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(CharlaPProg charlaPProg) {
			UpdateOperation.this.update( new Object[]{				
				charlaPProg.getIdTipoCharla(),
				charlaPProg.getIdTipoDtoStr(),
				charlaPProg.getDocumentoStr(),				
				charlaPProg.getTema(),
				charlaPProg.getDescripcion(),
				charlaPProg.getFecha(),
				charlaPProg.getHora(),
				charlaPProg.getIdLugar(),
				charlaPProg.getOtro(),
				charlaPProg.getUnidadDuracion(),
				charlaPProg.getDuracion(),
				charlaPProg.getEstado(),			
				charlaPProg.getUsuarioModifica(),
				charlaPProg.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_CHARLA_P_PROG_T "
										+ WHERE + "1=1";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(CharlaPProg charlaPProg) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_CHARLA_P_PROG_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			CharlaPProg charlaPProg = new CharlaPProg();
				charlaPProg.setId(new Long(rs.getLong("ID")));
				charlaPProg.setIdTipoCharla(new Long(rs.getLong("ID_TIPO_CHARLA")));
				charlaPProg.setIdTipoCharlaStr(rs.getString("ID_TIPO_CHARLA"));
				charlaPProg.setIdTipoDtoStr(rs.getString("TIPO_DTO_FUNCIONARIO"));
				charlaPProg.setDocumentoStr(rs.getString("DTO_FUNCIONARIO"));
				charlaPProg.setTema(rs.getString("TEMA"));
				charlaPProg.setDescripcion(rs.getString("DESCRIPCION"));
				charlaPProg.setFecha(rs.getDate("FECHA"));		
				charlaPProg.setHora(rs.getString("HORA"));
				charlaPProg.setIdLugar(new Long(rs.getLong("ID_LUGAR")));
				charlaPProg.setIdLugarStr(rs.getString("ID_LUGAR"));
				charlaPProg.setOtro(rs.getString("OTRO_LUGAR"));
				charlaPProg.setUnidadDuracion(new Long(rs.getLong("UNIDAD_DURACION")));
				charlaPProg.setUnidadDuracionStr(rs.getString("UNIDAD_DURACION"));
				charlaPProg.setDuracion(new Long(rs.getLong("DURACION")));
				charlaPProg.setDuracionStr(rs.getString("DURACION"));
				charlaPProg.setEstado(rs.getString("ESTADO"));
				charlaPProg.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				charlaPProg.setFechaCrea(rs.getDate("FECHA_CREA"));		
				charlaPProg.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				charlaPProg.setFechaModifica(rs.getDate("FECHA_MODIFICA"));

			return charlaPProg;
		}
    }
	
	 // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class ValidarDocumentoOperation extends MappingSqlQuery {
		public ValidarDocumentoOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			
				String existe = rs.getString("EXISTE");
			return existe;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public CharlaPProg runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (CharlaPProg) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_TIPO_CHARLA = ? "
			  	  	  					//+ " AND ID_FUNCIONARIO = ? "
			  	  	  					//+ " AND DESCRIPCION = ? "
			  	  	  					//+ " AND FECHA = ? "
			  	  	  					//+ " AND HORA = ? "
			  	  	  					//+ " AND ID_LUGAR = ? "
			  	  	  					//+ " AND UNIDAD_DURACION = ? "
			  	  	  					//+ " AND DURACION = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public CharlaPProg runQuery(CharlaPProg charlaPProg) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//charlaPProg.getId(),
				//charlaPProg.getIdTipoCharla(),
				//charlaPProg.getIdFuncionario(),
				//charlaPProg.getDescripcion(),
				//charlaPProg.getFecha(),
				//charlaPProg.getHora(),
				//charlaPProg.getIdLugar(),
				//charlaPProg.getUnidadDuracion(),
				//charlaPProg.getDuracion(),
				//charlaPProg.getEstado(),
				//charlaPProg.getUsuarioCrea(),
				//charlaPProg.getFechaCrea(),
				//charlaPProg.getUsuarioModifica(),
				//charlaPProg.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (CharlaPProg) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    
    
    // ------------- FindAllQuery ----------------------------------------    
    class FindByTipoCharlaQuery extends BaseQueryOperation {
		private static final String sql = "SELECT * " 
										+ " FROM OPE_CHARLA_P_PROG_T CH "										
										+ " WHERE ID_TIPO_CHARLA =? ";
    	public FindByTipoCharlaQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(CharlaPProg charlaPProg) {
			List result =  execute( new Object[]{
					charlaPProg.getIdTipoCharlaStr()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    
    
    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public CharlaPProg runQuery(CharlaPProg charlaPProg) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (CharlaPProg) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByDocumentoQuery ----------------------------------------    
    class VerificarDocumentoQuery extends ValidarDocumentoOperation {
		private static final String sql = "SELECT COALESCE(COUNT(*),0) AS EXISTE  " +
											" FROM ADM_PERSONA_T WHERE IDENTIFICACION =? AND "
										 	+ " ID_TIPO_IDENTIFICACION=? ";
										 
    	public VerificarDocumentoQuery(DataSource ds) {
    		super(ds,sql);			
			declareParameter( new SqlParameter(Types.CHAR) );
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public String runQuery(CharlaPProg charlaPProg) {
			List result =  execute( new Object[]{							
					charlaPProg.getDocumentoStr(),
					charlaPProg.getIdTipoDtoStr()
			});
			if ( result.size() > 0 ) {
        		return  result.get(0).toString();
        	} else {
        		return null;
        	}
    	}
    }    
    
}
